<?php

/**
 * Description of SiteController
 *
 * @author Eureka
 */
class SiteController extends CController
{

	//put your code here
	public function actionIndex()
	{
		$sql = 'select * from articles';
//		$dsn = 'mysql:host=localhost;dbname=butchiso';
//		$username = 'root';
//		$password = '';
//		try
//		{
//			//$connection = new CDbConnection($dsn, $username, $password);
//			$connection = Yii::app()->db;
//			$command = $connection->createCommand($sql);
//			$dataReader = $command->query();
//			// calling read() repeatedly until it returns falsea
//			foreach ($dataReader as $row)
//			{
//				echo $row['title'] . '<br/>';
//			}
//		} catch (CDbException $ex)
//		{
//			echo $ex;
//		}
		//$article = Articles::model()->findAllByPk(5);
		//var_dump($article);

		$articles = Articles::model()->findAll();

		$item_count = Articles::model()->count();
		$page_size = 2;

		$pages = new CPagination($item_count);
		$pages->setPageSize($page_size);

		$this->render('index', array(
			'articles' => $articles,
			'pages' => $pages
		));

//		$newRecord = new Articles();
//		$newRecord->title = "test";
//		$newRecord->body = "description test";
//		$newRecord->save();
//		$dataProvider = new CActiveDataProvider('Articles', array(
//					'pagination' => array(
//						'pageSize' => 2,
//					),
//				));
//
//		$this->render('index', array(
//			'dataProvider' => $dataProvider,
//		));
	}

	public function actionCreate()
	{
		$model = new ArticlesForm();
		if (isset($_POST['ArticlesForm']))
		{
			$model->attributes = $_POST['ArticlesForm'];
			if ($model->validate())
			{
				echo $model->title;
				echo $model->body;
				Yii::app()->user->setFlash('create','Thank you for contacting us. We will respond to you as soon as possible.');
			}
		}
		$this->render('create', array('model' => $model));
	}

}

?>
